# Enable mouse mode
#set -g mouse on

# make scrolling with wheels work
#bind -n WheelUpPane if-shell -F -t = "#{mouse_any_flag}" "send-keys -M" "if -Ft= '#{pane_in_mode}' 'send-keys -M' 'select-pane -t=; copy-mode -e; send-keys -M'"
#bind -n WheelDownPane select-pane -t= \; send-keys -M

setw -g mode-keys vi
bind-key -t vi-copy 'v' begin-selection
bind-key -t vi-copy 'y' copy-selection

set-window-option -g mode-keys vi
set-window-option -g automatic-rename on
set-option -g status-keys vi
set-option -g history-limit 10000
set-option -g base-index 1
set-option -s escape-time 0

# 256 colors for vim
set -g default-terminal "screen-256color"
#bind -n WheelUpPane   select-pane -t= \; copy-mode -e \; send-keys -M
#bind -n WheelDownPane select-pane -t= \;                 send-keys -M
#set-option -g -q mouse on
#bind-key -T root PPage if-shell -F "#{alternate_on}" "send-keys PPage" "copy-mode -e; send-keys PPage"
bind-key -t vi-copy PPage page-up
bind-key -t vi-copy NPage page-down
set-option -ga terminal-override ',rxvt-uni*:XT:Ms=\E]52;%p1%s;%p2%s\007'

# Scrolling works as expected
set -g terminal-overrides 'xterm*:smcup@:rmcup@'

# change prefix to Ctrl-\
# unbind-key C-b
# set-option -g prefix 'C-\'

# resize panes like vim
bind < resize-pane -L 10
bind > resize-pane -R 10
bind - resize-pane -D 10
bind + resize-pane -U 10

# move x clipboard into tmux paste buffer
#bind C-p run "tmux set-buffer \"$(xclip -o)\";
#tmux paste-buffer"
# move tmux copy buffer into x clipboard
#bind C-y run "tmux show-buffer | xlip -i"


# vim's definition of a horizontal/vertical split is reversed from tmux's
bind s splitw -v 
bind v splitw -h

# hit C-\ twice to go to last window
bind-key C-\ last-window
bind-key b set-option status

bind-key / command-prompt "split-window 'exec man %%'"

# move around panes with hjkl
bind k selectp -U   
bind j selectp -D   
bind h selectp -L   
bind l selectp -R 

# C-l is taken over by vim style pane navigation
bind C-l send-keys 'C-l'

# Use vim keybindings in copy mode
setw -g mode-keys vi

# Setup 'v' to begin selection as in Vim
bind-key -t vi-copy v begin-selection
bind-key -t vi-copy y copy-pipe "reattach-to-user-namespace pbcopy"

# Update default binding of `Enter` to also use copy-pipe
unbind -t vi-copy Enter
bind-key -t vi-copy Enter copy-pipe "reattach-to-user-namespace pbcopy"

# Smart pane switching with awareness of vim splits
# See: https://github.com/christoomey/vim-tmux-navigator
is_vim='echo "#{pane_current_command}" | grep -iqE "(^|\/)g?(view|n?vim?x?)(diff)?$"'
bind -n C-h if-shell "$is_vim" "send-keys C-h" "select-pane -L"
bind -n C-j if-shell "$is_vim" "send-keys C-j" "select-pane -D"
bind -n C-k if-shell "$is_vim" "send-keys C-k" "select-pane -U"
bind -n C-l if-shell "$is_vim" "send-keys C-l" "select-pane -R"
bind -n C-\ if-shell "$is_vim" "send-keys C-\\" "select-pane -l"
